package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.UserDailyStatManager;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.stat.UserDailyStatMapper;
import com.net.xpay.common.domain.stat.UserDailyStat;
import com.net.xpay.common.model.AggMonthUserDailyStatModel;
import com.net.xpay.common.model.AggOperatorUserDailyStatModel;
import com.net.xpay.common.model.AggUserDailyStatModel;
import com.net.xpay.common.model.search.OperatorUsersSearchModel;
import com.net.xpay.core.dto.SafePayBookAccountCountDto;
import com.net.xpay.core.model.UserDailyStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class UserDailyStatManagerImpl extends AbstractManager<UserDailyStat> implements UserDailyStatManager {

    @Autowired
    private UserDailyStatMapper mapper;


    @Override
    protected BasicMapper<Long, UserDailyStat> getMapper() {
        return mapper;
    }

    @Override
    public UserDailyStat findByDayAndUserId(LocalDate day, Long userId) {
        UserDailyStat condition = new UserDailyStat();
        condition.setDay(day);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<AggUserDailyStatModel> aggSearch(UserDailyStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("user_id", "desc");
        List<AggUserDailyStatModel> aggUserDailyStatModels = mapper.aggSearch(searchModel, page);
        return aggUserDailyStatModels  == null ? new ArrayList<>() : aggUserDailyStatModels;
    }

    @Override
    public long aggSearchCount(UserDailyStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.aggSearchCount(searchModel);
    }

    @Override
    public List<AggUserDailyStatModel> daySearch(LocalDate yesterday,LocalDate dayBefore ) {
        Preconditions.checkArgument(yesterday != null || dayBefore != null);
        List<AggUserDailyStatModel> aggUserDailyStatModels = mapper.daySearch(yesterday, dayBefore);
        return aggUserDailyStatModels == null ? new ArrayList<>() : aggUserDailyStatModels;
    }

    @Override
    public List<AggMonthUserDailyStatModel> listByData(Integer year, Integer month) {
        Preconditions.checkArgument(year != null || month != null);
        return mapper.listByData(year,month);
    }

    @Override
    public AggMonthUserDailyStatModel sumByUserIdAndData(Long userId, Integer year, Integer month) {
        Preconditions.checkArgument(userId !=null ||year != null || month != null);
        return mapper.sumByUserIdAndData(userId,year,month);
    }

    @Override
    public AggOperatorUserDailyStatModel sumByUserIdAndSearchModel(Long userId,OperatorUsersSearchModel searchModel) {
        Preconditions.checkArgument(userId !=null || searchModel != null);
        return mapper.sumByUserIdAndSearchModel(userId,searchModel);
    }

    @Override
    public List<UserDailyStat> listByDay(LocalDate day) {
        Preconditions.checkArgument(day != null);
        return mapper.listByDay(day);
    }

    @Override
    public SafePayBookAccountCountDto sumMonthAmount(LocalDateTime startMonthTime, LocalDateTime startTime, Long userId) {
        Preconditions.checkArgument(startMonthTime != null);
        SafePayBookAccountCountDto safePayBookAccountCountDto = mapper.sumMonthAmount(startMonthTime,startTime,userId);
        return safePayBookAccountCountDto == null ? new SafePayBookAccountCountDto() : safePayBookAccountCountDto;
    }


}
